/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;
import util.filter.Predicate;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_educational_info_requeriment_and_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchEducationalInfoRequerimentAndOperator.findAll", query = "SELECT s FROM SearchEducationalInfoRequerimentAndOperator s"),
    @NamedQuery(name = "SearchEducationalInfoRequerimentAndOperator.findByIdSearchEducationalInfoRequerimentAndOperator", query = "SELECT s FROM SearchEducationalInfoRequerimentAndOperator s WHERE s.idSearchEducationalInfoRequerimentAndOperator = :idSearchEducationalInfoRequerimentAndOperator")})
public class SearchEducationalInfoRequerimentAndOperator implements Serializable,Predicate<Candidate> {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_EDUCATIONAL_INFO_REQUERIMENT_AND_OPERATOR")
    private Integer idSearchEducationalInfoRequerimentAndOperator;
    @JoinColumn(name = "SECTOR", referencedColumnName = "ID_SECTOR")
    @ManyToOne(fetch = FetchType.LAZY)
    private Sector sector;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "MINIMUM_EDUCATIONAL_LEVEL", referencedColumnName = "ID_EDUCATIONAL_LEVEL")
    @ManyToOne(fetch = FetchType.LAZY)
    private EducationalLevel minimumEducationalLevel;

    public SearchEducationalInfoRequerimentAndOperator() {
    }

    public SearchEducationalInfoRequerimentAndOperator(Integer idSearchEducationalInfoRequerimentAndOperator) {
        this.idSearchEducationalInfoRequerimentAndOperator = idSearchEducationalInfoRequerimentAndOperator;
    }

    public Integer getIdSearchEducationalInfoRequerimentAndOperator() {
        return idSearchEducationalInfoRequerimentAndOperator;
    }

    public void setIdSearchEducationalInfoRequerimentAndOperator(Integer idSearchEducationalInfoRequerimentAndOperator) {
        this.idSearchEducationalInfoRequerimentAndOperator = idSearchEducationalInfoRequerimentAndOperator;
    }

    public Sector getSector() {
        return sector;
    }

    public void setSector(Sector sector) {
        this.sector = sector;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public EducationalLevel getMinimumEducationalLevel() {
        return minimumEducationalLevel;
    }

    public void setMinimumEducationalLevel(EducationalLevel minimumEducationalLevel) {
        this.minimumEducationalLevel = minimumEducationalLevel;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchEducationalInfoRequerimentAndOperator != null ? idSearchEducationalInfoRequerimentAndOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean sectorRes=false;
        boolean minimumEducationalLevelRes=false;
        if (!(object instanceof SearchEducationalInfoRequerimentAndOperator)) {
            return false;
        }
        SearchEducationalInfoRequerimentAndOperator other = (SearchEducationalInfoRequerimentAndOperator) object;
        
        //Sector test
        if(this.sector==null && other.getSector()==null){
            sectorRes=true;
        }
        else if(this.sector!=null && other.getSector()!=null){
            sectorRes=this.sector.equals(other.getSector());
        }
        //MinimumEducationalLevel test
        if(this.getMinimumEducationalLevel()==null && other.getMinimumEducationalLevel()==null){
            minimumEducationalLevelRes=true;
        }
        else if(this.getMinimumEducationalLevel()!=null && other.getMinimumEducationalLevel()!=null){
            minimumEducationalLevelRes=this.getMinimumEducationalLevel().equals(other.getMinimumEducationalLevel());
        }
        
        Boolean res=sectorRes && minimumEducationalLevelRes;
        return res;
    }

    @Override
    public String toString() {
        return "entities.SearchEducationalInfoRequerimentAndOperator[ idSearchEducationalInfoRequerimentAndOperator=" + idSearchEducationalInfoRequerimentAndOperator + " ]";
    }

    @Override
    public boolean test(Candidate c) {
        boolean pass=false;
        if(c.getCandidateEducationalInfoList()!=null){
            for(CandidateEducationalInfo cei: c.getCandidateEducationalInfoList()){
                boolean validSector=getSector()==null;
                boolean validMinimumEducationalLevel=getMinimumEducationalLevel()==null;
                if(getSector()!=null && cei.getSector()!=null){
                    validSector=getSector().equals(cei.getSector());
                }
                if(getMinimumEducationalLevel()!=null && cei.getEducationalLevel()!=null){
                    validMinimumEducationalLevel=getMinimumEducationalLevel().getEducationalValue()<=cei.getEducationalLevel().getEducationalValue();
                }
                pass=validSector && validMinimumEducationalLevel;
                if(pass){
                    break;
                }
            }
        }
        return pass;
    }
    
}
